package com.hsy.bibc.modules.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.widget.ImageButton;

import com.deparse.dpframe.ui.widget.DPViewPager;
import com.deparse.dpframe.ui.widget.DPagerSlidingTabStrip;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.modules.main.MainActivity;
import com.hsy.bibc.modules.order.frament.FragmentExChangeList;
import com.hsy.bibc.modules.order.frament.FragmentSpendList;
import com.hsy.bibc.modules.order.frament.FragmentWasteList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/2.
 */
public class OrderActivity extends CommonBaseActivityWithSimpleActionBar {

    private DPViewPager activity_order_pager;
    private List<Fragment> myFragmentList;
    private DPagerSlidingTabStrip tabs;
    private MyPagerAdapter adapter;
    private String type;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (type.equals("CommitOk")) {

                startActivityWithTaskCleared(MainActivity.class);
                return false;

            } else {
                finish();
                return false;
            }


        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    public boolean onActionBarLeftButtonClicked(ImageButton leftButton) {
        if (type.equals("CommitOk")) {
            startActivityWithTaskCleared(MainActivity.class);
        } else {
            finish();
        }
        return super.onActionBarLeftButtonClicked(leftButton);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.title_my_order);
        setContentView(R.layout.activity_order);
        type = getActivityStartExtra()[0];
        initBody();
    }

    private void initBody() {
        activity_order_pager = F(R.id.activity_order_pager);
        tabs = F(R.id.tabs);

        myFragmentList = new ArrayList<Fragment>();
        FragmentWasteList fragmentWasteList = new FragmentWasteList();
        FragmentExChangeList fragmentExChangeList = new FragmentExChangeList();
        FragmentSpendList fragmentSpendList = new FragmentSpendList();
        fragmentWasteList.setAttchedActivity(this);
        fragmentExChangeList.setAttchedActivity(this);
        fragmentSpendList.setAttchedActivity(this);

        myFragmentList.add(fragmentWasteList);
        myFragmentList.add(fragmentExChangeList);
        myFragmentList.add(fragmentSpendList);

        adapter = new MyPagerAdapter(getSupportFragmentManager(), myFragmentList);

//        tabs.setIndicatorWidth(DPUnitUtil.dip2px(this, 80));

        int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        activity_order_pager.setAdapter(adapter);
        activity_order_pager.setPageMargin(pageMargin);
        tabs.setViewPager(activity_order_pager);

        tabs.setOnPageChangeListener(new DPViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

//                if (myFragmentList.get(position) instanceof IFragmentUpdateListener) {
//                    ((IFragmentUpdateListener) myFragmentList.get(position)).onFragmentUpdate();
//                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"废品回收", "积分兑换", "买单收银"};
        private List<Fragment> myFragmentsList;

        public MyPagerAdapter(FragmentManager fragmentmanager, List<Fragment> list) {
            super(fragmentmanager);
            myFragmentsList = list;
        }

        public int getItemPosition(Object obj) {
            return super.getItemPosition(obj);
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return myFragmentsList.size();
        }

        public Fragment getItem(int i) {
            return (Fragment) myFragmentsList.get(i);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

    }
}
